#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;

#define N 26

int tree[N];

int find_root(int x);

class graph_edge {
public:
    int a, b;
    double cost;

    bool operator<(const graph_edge &e) const {
        return cost < e.cost;
    }
} edge[6000];

int main(int argc, char const *argv[]) {
    int n;
    while (cin >> n && n != 0) {
        int edge_count = 0;
        for (int i = 0; i < n-1; ++i){
            char node;
            int num;
            cin >> node >> num;
            for (int j = 0; j < num; j++){
                char node_2;
                int costs;
                cin >> node_2 >> costs;
                edge[edge_count].a = node - 'A';
                edge[edge_count].b = node_2 - 'A';
                edge[edge_count++].cost = costs;
            }
        }

        sort(edge, edge + edge_count);
        for (int i = 0; i < n; ++i)
            tree[i] = -1;

        int result = 0;
        for (int i = 0; i < edge_count; ++i) {
            int a_inedx = find_root(edge[i].a);
            int b_index = find_root(edge[i].b);
            if (a_inedx != b_index) {
                tree[a_inedx] = b_index;
                result += edge[i].cost;
            }
        }
        printf("%d\n", result);
    }
    return 0;
}

int find_root(int x) {
    if (tree[x] == -1)
        return x;
    else {
        int temp_index = find_root(tree[x]);
        tree[x] = temp_index;
        return temp_index;
    }
}